package com.showmaker.service.impl;

import com.showmaker.bean.OrderSetting;
import com.showmaker.mapper.OrderSettingMapper;
import com.showmaker.service.OrderSettingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Transactional
@DubboService(interfaceClass = OrderSettingService.class, version = "1.0")
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> list) {
        // 判断传入进来的集合是否为空 或 长度大于0
        if (list != null && list.size() > 0) {
            // 遍历结合中的每个元素
            for (OrderSetting orderSetting : list) {
                // 查询这一天是否已经设置了预约信息
                // 如果已经设置了信息，则变为修改操作；如果没有设置相关信息则变为添加操作
                long count = orderSettingMapper.count(orderSetting.getOrderDate());
                if (count > 0) {
                    orderSettingMapper.edit(orderSetting);
                } else {
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSetting(String date) {
        // 构造开始日期和结束日期，将所有的月份都当做31天来计算
        String begin = date + "-1";

        // 本月最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String end = format.format(time);

        HashMap<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);

        // 调用mapper层，根据日期访问查询预约设置数据
        List<OrderSetting> list = orderSettingMapper.getOrderSetting(map);

        // 对返回的结果进行再次封装，也就是将 OrderSetting 中的数据进行整理
        // 返回符合前端页面需要的数据
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() != 0) {
            for (OrderSetting orderSetting : list) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("date", orderSetting.getOrderDate().getDate());
                hashMap.put("number", orderSetting.getNumber());
                hashMap.put("reservations", orderSetting.getReservations());
                result.add(hashMap);
            }
        }

        return result;
    }

    @Override
    public int setPreNumber(Map<String, Object> map) {
        // 获取要查询的日期
        String date = (String) map.get("date");

        // 查询当前日期在数据库中是否有设置预约人数
        int preOrderNumber = orderSettingMapper.insertPreOrderNumber(map);
        // 根据查询的结果决定是执行添加操作还是执行修改操作
        if (preOrderNumber > 0) {
            // 如果当前日期已经设置过了预约人数，则进行修改操作
            return orderSettingMapper.updatePreOrderNumber(map);
        } else {
            // 如果当前日期并没有设置过预约人数，则直接添加一条数据
            return orderSettingMapper.insertPreOrderNumber(map);
        }
    }
}
